﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class GroupShowController : BenefitHelper.Tools.BaseControl
    {
        public ActionResult IndexShow(int uid, string pdate)
        {
            string stringPdate = new BenefitHelper.Sys.TradeHistory().AddHorizontalLineIntoPdate(pdate);//为日期加上横线
            ViewBag.Hpdate = stringPdate;//保存该横线日期供页面显示调用

            ViewBag.gid = uid;
            ViewBag.pdate = pdate;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            BenefitHelper.Sys.Groups group = new BenefitHelper.Sys.Groups().GetGroup(uid, db);
            ViewBag.group = group;

            if (BenefitHelper.Sys.VisitPrower.HasGroupPrower(operId, uid, db))
            {

                cookie.SetCookie("groupquerydate", pdate, 24);
                ViewBag.groupquerydate = cookie.GetCookie("groupquerydate");
                //    cookie.SetCookie("groupquerydate", cookie.GetCookie("groupquerydate"), 24);
                string js = "";
                BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                th = th.GetSettlementModel(pdate, db);

                List<BenefitHelper.Sys.Operator> operators = new BenefitHelper.Sys.DayGroupOperators().GetGroupOperators(uid, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate), db);
                List<BenefitHelper.View.OperatorDayChange> changes = new List<BenefitHelper.View.OperatorDayChange>();
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorDayChange change = new BenefitHelper.View.OperatorDayChange().GetOperatorDayChange(op.Id, th.Id, db);
                    changes.Add(change);
                }
                ViewBag.changes = changes;


                //周累计
                List<BenefitHelper.View.OperatorWeekSum> weekchanges = new List<BenefitHelper.View.OperatorWeekSum>();
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorWeekSum change = new BenefitHelper.View.OperatorWeekSum().GetOperatorWeekSum(op.Id, th.YPdate, th.MPdate, th.WPdate, db);
                    weekchanges.Add(change);
                }
                ViewBag.weekchanges = weekchanges;

                //月累计
                List<BenefitHelper.View.OperatorMonthSum> monthchanges = new List<BenefitHelper.View.OperatorMonthSum>();
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorMonthSum change = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(op.Id, th.MPdate, th.YPdate, db);
                    monthchanges.Add(change);
                }
                ViewBag.monthchanges = monthchanges;

                //周预期
                List<BenefitHelper.View.OperatorWeekExpected> weekexpectedchanges = new List<BenefitHelper.View.OperatorWeekExpected>();
                foreach (BenefitHelper.Sys.Operator op in operators)
                {
                    BenefitHelper.View.OperatorWeekExpected change = new BenefitHelper.View.OperatorWeekExpected().GetOperatorWeekExpected(op.Id, th.WPdate, th.YPdate, db);
                    weekexpectedchanges.Add(change);
                }
                ViewBag.weekexpectedchanges = weekexpectedchanges;



                BenefitHelper.Data.Day.GroupDayChange groupchange = new BenefitHelper.Data.Day.GroupDayChange();
                groupchange = groupchange.GetGroupDayChange(uid, th.Id, db);//查询当日交易数据ok
                ViewBag.change = groupchange;//保存当日交易数据供页面调用

                BenefitHelper.Data.Year.GroupAvgYear groupavgyear = new BenefitHelper.Data.Year.GroupAvgYear().GetGroupAvgYear(uid, th.YPdate, db);//查询小组年均值ok
                ViewBag.groupavgyear = groupavgyear;//保存小组年均值数据供页面调用

                BenefitHelper.View.GroupFiveDayAvg changefiveday = new BenefitHelper.View.GroupFiveDayAvg().GetGroupFiveDayAvg(uid, th.Id, db);//五日均值ok
                ViewBag.changefiveday = changefiveday;//保存五日均值数据供页面调用

                BenefitHelper.Data.Month.GroupAvgMonth changemonth = new BenefitHelper.Data.Month.GroupAvgMonth().GetGroupAvgMonth(uid, th.MPdate, th.YPdate, db);//当月均值ok
                ViewBag.changemonth = changemonth;//保存当月均值数据供页面调用

                BenefitHelper.View.GroupDayAvg changegroup = new BenefitHelper.View.GroupDayAvg().GetGroupAvg(uid, th.Id, db);//查询小组均值
                ViewBag.changegroup = changegroup;//保存小组均值数据供页面调用

                BenefitHelper.Data.Day.CompanyAvgDay companyavgday = new BenefitHelper.Data.Day.CompanyAvgDay().GetCompanyAvgDay(th.Id, db);//查询公司均值
                ViewBag.companyavgday = companyavgday;//保存公司均值数据供页面调用

                //BenefitHelper.Data.Day.BatchAvgDay batchavgday = new BenefitHelper.Data.Day.BatchAvgDay().GetBatchAvgDay(operatorId, tid, db);//查询同批次交易员均值
                //ViewBag.batchavgday = batchavgday;//保存同批次交易员均值数据供页面调用

                BenefitHelper.Data.Week.GroupWeekSum operatorweeksum = new BenefitHelper.Data.Week.GroupWeekSum().GetGroupWeekSum(uid, th.WPdate, th.YPdate, db);//查询小组周累计
                ViewBag.operatorweeksum = operatorweeksum;//保存小组周累计数据供页面调用

                BenefitHelper.Data.Month.GroupMonthSum groupmonthsum = new BenefitHelper.Data.Month.GroupMonthSum().GetGroupMonthSum(uid, th.MPdate, th.YPdate, db);//查询小组月累计ok
                ViewBag.operatormonthsum = groupmonthsum;//保存小组月累计数据供页面调用

                BenefitHelper.Data.Year.GroupYearSum groupyearsum = new BenefitHelper.Data.Year.GroupYearSum().GetGroupYearSum(uid, th.YPdate, db);//查询小组年累计ok
                ViewBag.operatoryearsum = groupyearsum;//保存小组年累计数据供页面调用


                js += BenefitHelper.Tools.ChartAtManager.GroupBar(uid, th.Id, db);
                js += BenefitHelper.Tools.ChartAtManager.GroupDayDiffBar(uid, th.Id, db); ;
                //   js += BenefitHelper.Tools.ChartAtManager.ProductPie3();
                ViewBag.js = js;
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
            return View();
        }

    }
}
